<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
<!--    &lt;!&ndash; 搜索筛选 &ndash;&gt;-->
<!--    <el-form :inline="true" :model="formInline" class="user-search">-->
<!--      <el-form-item label="搜索：">-->
<!--        &lt;!&ndash; <el-input size="small" v-model="formInline.varName" placeholder="输入用户名"></el-input> &ndash;&gt;-->
<!--      </el-form-item>-->
<!--      <el-form-item label="">-->
<!--        &lt;!&ndash; 输入用户ID进行搜索 &ndash;&gt;-->
<!--        <el-input size="small" v-model="formInline.varLable" placeholder="输入用户id"></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        &lt;!&ndash; 点击搜索按钮触发 search 方法 &ndash;&gt;-->
<!--        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->
    >
    <el-table size="small" :data="listData" v-loading="loading" border>
      <el-table-column align="center" type="selection" width="60"></el-table-column>
      <el-table-column prop="user_id" label="用户ID" width="150"></el-table-column>
      <el-table-column prop="user_name" label="用户名" width="150"></el-table-column>
      <el-table-column prop="user_class" label="用户类别" width="150"></el-table-column>
      <el-table-column prop="user_password" label="用户密码" width="150"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
  </div>
</template>

<script>
import { userList, userDetail, userDelete } from '../../api/basisMG';
import Pagination from '../../components/Pagination';
export default {
  data() {
    return {
      loading: false,
      formInline: {
        page: 1,
        limit: 10,
        varLable: '',
        varName: '',
        token: localStorage.getItem('logintoken')
      },
        userparm: [], //搜索权限
        listData: [], //用户数据
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
      }
    };
  },
  components: {
    Pagination
  },
  created() {
    userList().then((response) => {
      // 将获取到的数据存储到 listData
      this.listData = response.data;
    });
  },
  // 获取用户列表
  methods: {
    getdata() {
      this.loading = true;

      userList()
        .then(res => {
          this.loading = false;
          this.listData = res.data;
        })
        .catch(err => {
          this.loading = false;
          this.$message.error('获取用户列表失败，请稍后再试！');
        });
    }, pageparm: {
      currentPage: 1,
      pageSize: 10,
      total: 10
    },
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 调用删除接口，传入用户的 ID
          userDelete(row.user_id)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '用户已删除!'
                });
                this.getdata();
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                });
              }
            })
            .catch(err => {
              this.loading = false;
              this.$message.error('用户删除失败，请稍后再试！');
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      search()
      {
        // 调用搜索接口，传入用户ID
        userDetail(this.formInline.varLable)
          .then(res => {
            if (res.success) {
              // 清空列表数据
              this.listData = [];
              // 将搜索结果添加到列表数据中
              this.listData.push(res.data);
            } else {
              this.$message({
                type: 'info',
                message: '未找到相关用户'
              });
            }
          })
          .catch(err => {
            this.$message.error('用户搜索失败，请稍后再试！');
          });
      }
    }

}}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>


